package com.KKB.web;

import com.KKB.bean.Menu;
import com.KKB.bean.Role;

import com.KKB.service.MenuService;
import com.KKB.service.RoleService;

import com.KKB.service.impl.MenuServiceImpl;
import com.KKB.service.impl.RoleServiceImpl;

import com.KKB.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/role/role")
public class RoleServlet extends HttpServlet {
    private RoleService service=new RoleServiceImpl();
    private MenuService menuService=new MenuServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("select".equals(method)){
            select(req,resp);
        }else if ("selectmenus".equals(method)){
            selectmenu(req,resp);
        }else if ("insert".equals(method)){
            insert(req,resp);
        }else if ("update".equals(method)){
            update(req,resp);
        }else if ("findById".equals(method)){
            findById(req,resp);
        }else if ("delete".equals(method)){
            delete(req,resp);
        }
    }
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rolename = req.getParameter("rolename");
        String state = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        int i = service.insertRole(rolename, state, menuids);
        if (i>0){
            resp.sendRedirect("/power/role/role?method=select");
        }else {
            resp.sendRedirect("/power/role/role?method=selectmenus");
        }
    }
    protected void selectmenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist",menuList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }

    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);

        PageUtil pageUtil = new PageUtil();
        List<Role> roleList = service.getRoleList(pageIndex, pageUtil.getPageSize());
        int total = service.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(roleList);

        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
    // 更新角色
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        String roleName = req.getParameter("roleName");
        String roleState = req.getParameter("roleState");
        String[] menuIds = req.getParameterValues("menuId");
        System.out.println(menuIds);
        Role role = new Role();
        role.setRoleId(Integer.parseInt(roleId));
        role.setRoleName(roleName);
        role.setRoleState(Integer.parseInt(roleState));

        RoleService roleService = new RoleServiceImpl();
        int i = roleService.update(role, menuIds);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.println("<script>alert('更新成功');location.href='/power/role/role?method=select';</script>");
        } else {
            writer.println("<script>alert('更新失败');location.href='/power/role/role?method=findById&rid"+roleId+"';</script>");
        }

    }
    // 主键查询
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        RoleService roleService = new RoleServiceImpl();
        Role role = roleService.findById(Integer.parseInt(rid));

        MenuService menuService = new MenuServiceImpl();
        List<Menu> menus = menuService.getMenuList();


        req.setAttribute("r1", role);
        req.setAttribute("menus", menus);

        req.getRequestDispatcher("edit.jsp").forward(req, resp);
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        RoleService roleService = new RoleServiceImpl();
        int i = roleService.deleteById(Integer.parseInt(rid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        if (i > 0) {
            writer.println("删除成功");
        } else {
            writer.println("删除失败");
        }
    }



}
